package org.wsff.tools.api.gateway.core;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.wsff.tools.api.gateway.error.GatewayErrorFactory;
import org.wsff.tools.api.gateway.error.ValidationException;

/**
 * 服务请求验证器抽象类
 *
 * @author ryan
 * @version Id: AbstractProxyValidator<R.java, v 0.1 2022-01-19 11:33 ryan Exp $$
 */
public abstract class AbstractProxyValidator<Request extends Serializable> implements ProxyValidator<Request> {

    private final GatewayErrorFactory ERROR = GatewayErrorFactory.getInstance();

    /**
     * 参数校验方法
     *
     * @param request 请求
     * @throws ValidationException 异常
     */
    @Override
    public void validate(Request request) throws ValidationException {
        doValidate(request);
    }

    /**
     * 执行业务校验
     *
     * @param request 请求参数
     * @throws ValidationException 验证异常
     */
    protected abstract void doValidate(Request request) throws ValidationException;

    /**
     * 参数不为真
     *
     * @param value 入参
     * @param message 校验异常
     * @throws ValidationException 验证异常
     */
    protected void paramNotTrue(boolean value, String message) throws ValidationException {
        if (!value) {
            throw new ValidationException(ERROR.paramNull(message));
        }
    }

    /**
     * 参数不为空
     *
     * @param value 入参
     * @throws ValidationException 校验异常
     */
    protected void paramNotNull(Object value, String message) throws ValidationException {
        if (value == null) {
            throw new ValidationException(ERROR.paramNull(message));
        }
    }

    /**
     * 非法参数
     *
     * @param value 入参
     * @param message 参数名
     * @throws ValidationException 校验异常
     */
    protected void paramInvalid(Object value, String message) throws ValidationException {
        if (value == null) {
            throw new ValidationException(ERROR.invalidParamError(message));
        }
    }

    /**
     * 字符串不为空或空字符串
     *
     * @param value 入参
     * @throws ValidationException 校验异常
     */
    protected void paramNotStringEmpty(String value, String message) throws ValidationException {
        if (StringUtils.isBlank(value)) {
            throw new ValidationException(ERROR.paramNull(message));
        }
    }

    /**
     * 字符串不为空或空字符串
     *
     * @param value 入参
     * @throws ValidationException 校验异常
     */
    protected void paramNotStringBlank(String value, String message) throws ValidationException {
        if (StringUtils.isBlank(value)) {
            throw new ValidationException(ERROR.paramNull(message));
        }
    }

    /**
     * 集合不为空或空字符串
     *
     * @param value 入参
     * @throws ValidationException 校验异常
     */
    protected void paramNotCollectionEmpty(Collection<?> value, String message) throws ValidationException {
        if (value == null || value.isEmpty()) {
            throw new ValidationException(ERROR.paramNull(message));
        }
    }

    /**
     * map不为空或空字符串
     *
     * @param value 入参
     * @throws ValidationException 校验异常
     */
    protected void paramNotMapEmpty(Map value, String message) throws ValidationException {
        if (value == null || value.isEmpty()) {
            throw new ValidationException(ERROR.paramNull(message));
        }
    }

}
